package io.gbznt.modules.buss.service.impl;

import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gexin.rp.sdk.base.uitls.StringUtils;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussRtuSendRecordDao;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuSendRecordEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussRtuInfoService;
import io.gbznt.modules.buss.service.BussRtuSendRecordService;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.service.SysDeptService;

@Service("bussRtuSendRecordService")
public class BussRtuSendRecordServiceImpl extends ServiceImpl<BussRtuSendRecordDao, BussRtuSendRecordEntity>
		implements BussRtuSendRecordService {
	@Autowired
	private BussRtuInfoService BussRtuInfoService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private BussFarmlandInfoService bussFarmlandInfoService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<BussRtuSendRecordEntity> page = this.page(new Query<BussRtuSendRecordEntity>().getPage(params),
				new QueryWrapper<BussRtuSendRecordEntity>()
				// 个人数据权限控制
				.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
						"dept_id", sysDeptService.getPermissionDeptIdsArr(true,true))
				.orderByDesc("create_time")
		);

		for (BussRtuSendRecordEntity bussRtuSendRecordEntity : page.getRecords()) {
			if (StringUtils.isNotBlank(bussRtuSendRecordEntity.getRtuId())) {// 填充rtu序列号
				BussRtuInfoEntity rtu = BussRtuInfoService.getById(bussRtuSendRecordEntity.getRtuId());
				if (null != rtu) {
					bussRtuSendRecordEntity.setRtuXlh(rtu.getSerialNum());
				}
			}
			if (StringUtils.isNotBlank(bussRtuSendRecordEntity.getDeptId())) {// 填充部门
				SysDeptEntity dept = sysDeptService.getById(bussRtuSendRecordEntity.getDeptId());
				if (null != dept) {
					bussRtuSendRecordEntity.setDeptName(dept.getName());
				}
			}
			if (StringUtils.isNotBlank(bussRtuSendRecordEntity.getFarmlandInfoId())) {// 填充田块
				BussFarmlandInfoEntity tk = bussFarmlandInfoService
						.getById(bussRtuSendRecordEntity.getFarmlandInfoId());
				if (null != tk) {
					bussRtuSendRecordEntity.setTkName(tk.getName());
				}

			}

		}
		return new PageUtils(page);
	}

	@Override
	public List<BussRtuSendRecordEntity> getByTkId(Map<String, Object> params) {
		List<BussRtuSendRecordEntity> list = baseMapper.getByTkId(params);
		return list;
	}

}